package com.threeonemeeting.model;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author zhangzhongqing 已经下载的软件的描述信息
 */
public class DownloadInfoList {

	private List<DownloadInfo> listDownloadInfo = null;

	public int getSize() {
		return listDownloadInfo.size();
	}

	public int getFailedSize() {
		int nFailedNum = 0;

		for (int i = 0; i < listDownloadInfo.size(); i++) {
			DownloadInfo dInfo = listDownloadInfo.get(i);
			if (dInfo == null)
				continue;

			if (dInfo.nStatus == DownloadInfo.DOWNLOAD_STATUS_FAILED)
				nFailedNum++;
		}

		return nFailedNum;
	}

	public int getWiFiDownloadSize() {
		int nWiFiDownload = 0;

		for (int i = 0; i < listDownloadInfo.size(); i++) {
			DownloadInfo dInfo = listDownloadInfo.get(i);
			if (dInfo == null)
				continue;

			if (dInfo.nStatus == DownloadInfo.DOWNLOAD_STATUS_WAITINGWIFI)
				nWiFiDownload++;
		}

		return nWiFiDownload;
	}

	public int getRunningSize() {
		int nRunning = 0;

		for (int i = 0; i < listDownloadInfo.size(); i++) {
			DownloadInfo dInfo = listDownloadInfo.get(i);
			if (dInfo == null)
				continue;

			if (dInfo.nStatus == DownloadInfo.DOWNLOAD_STATUS_RUNNING)
				nRunning++;
		}

		return nRunning;
	}

	public DownloadInfo getAt(int i) {
		return listDownloadInfo.get(i);
	}

	public DownloadInfoList() {
		listDownloadInfo = new ArrayList<DownloadInfo>();
	}

	public DownloadInfo getDownloadInfo(String strAppKey) {
		if (strAppKey == null)
			return null;

		for (int i = 0; i < listDownloadInfo.size(); i++) {
			DownloadInfo info = listDownloadInfo.get(i);
			if (info != null
					&& info.strAppKey.equalsIgnoreCase(strAppKey) == true)
				return info;
		}

		return null;
	}

	public DownloadInfo getDownloadInfoByUrl(String strUrl) {
		if (strUrl == null)
			return null;

		for (int i = 0; i < listDownloadInfo.size(); i++) {
			DownloadInfo info = listDownloadInfo.get(i);
			if (info != null
					&& info.strFileUrl.equalsIgnoreCase(strUrl) == true)
				return info;
		}

		return null;
	}

	public DownloadInfo getDownloadInfoByPath(String strPath) {
		if (strPath == null)
			return null;

		for (int i = 0; i < listDownloadInfo.size(); i++) {
			DownloadInfo info = listDownloadInfo.get(i);
			if (info != null
					&& info.strLocalPath.equalsIgnoreCase(strPath) == true)
				return info;
		}

		return null;
	}

	public synchronized void addDownloadInfo(DownloadInfo data) {
		if (data == null)
			return;

		DownloadInfo dInfo = getDownloadInfo(data.strAppKey);
		if (dInfo == null) {
			listDownloadInfo.add(0, data);
		} else {
			dInfo = data;
		}
	}

	public synchronized void deleteDownloadInfo(String strAppKey) {
		if (strAppKey == null)
			return;

		for (int i = 0; i < listDownloadInfo.size(); i++) {
			DownloadInfo info = listDownloadInfo.get(i);
			if (info != null
					&& info.strAppKey.equalsIgnoreCase(strAppKey) == true)
				listDownloadInfo.remove(i);
		}
	}

	public ArrayList<DownloadInfo> getListArrayData() {
		return (ArrayList<DownloadInfo>) listDownloadInfo;
	}
}